package cn.elead.chaos.sso.server.web.system.controller;

import java.util.List;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.elead.chaos.excel.ExcelUtil;

import cn.elead.chaos.core.enums.BusinessType;
import cn.elead.chaos.framework.monitor.operlog.Log;
import cn.elead.chaos.framework.web.global.ApiAssert;
import cn.elead.chaos.framework.web.global.ErrorCodeEnum;
import cn.elead.chaos.framework.web.global.response.UnifiedReturn;
import cn.elead.chaos.mybatils.web.controller.BaseController;
import cn.elead.chaos.mybatils.web.page.TableDataInfo;
import cn.elead.chaos.shiro.util.ShiroUtils;
import cn.elead.chaos.sso.server.constants.UserConstants;
import cn.elead.chaos.sso.server.web.system.domain.SysPost;
import cn.elead.chaos.sso.server.web.system.service.ISysPostService;

/**
 * 岗位信息操作处理
 * 
 * @author ruoyi
 */
@Controller
@RequestMapping("/system/post")
public class SysPostController extends BaseController {
	private String prefix = "system/post";

	@Autowired
	private ISysPostService postService;

	@RequiresPermissions("system:post:view")
	@GetMapping()
	public String operlog() {
		return prefix + "/post";
	}

	@ResponseBody
	@UnifiedReturn
	@RequiresPermissions("system:post:list")
	@PostMapping("/list")
	public TableDataInfo<SysPost> list(SysPost post) {
		startPage();
		List<SysPost> list = postService.selectPostList(post);
		return getDataTable(list);
	}

	@ResponseBody
	@PostMapping("/export")
	@RequiresPermissions("system:post:export")
	@Log(title = "岗位管理", businessType = BusinessType.EXPORT)
	public String export(SysPost post) {
		List<SysPost> list = postService.selectPostList(post);
		ExcelUtil<SysPost> util = new ExcelUtil<SysPost>(SysPost.class);
		return util.exportExcel(list, "岗位数据");
	}

	@ResponseBody
	@PostMapping("/remove")
	@RequiresPermissions("system:post:remove")
	@Log(title = "岗位管理", businessType = BusinessType.DELETE)
	public void remove(String ids) throws Exception {
		postService.deletePostByIds(ids);
	}

	/**
	 * 新增岗位
	 */
	@GetMapping("/add")
	public String add() {
		return prefix + "/add";
	}

	/**
	 * 新增保存岗位
	 */
	@ResponseBody
	@PostMapping("/add")
	@RequiresPermissions("system:post:add")
	@Log(title = "岗位管理", businessType = BusinessType.INSERT)
	public void addSave(@Validated SysPost post) {
		if (UserConstants.POST_NAME_NOT_UNIQUE == postService.checkPostNameUnique(post)) {
			ApiAssert.failure(ErrorCodeEnum.BAD_REQUEST.convert("新增岗位'" + post.getPostName() + "'失败，岗位名称已存在"));
		} else if (UserConstants.POST_CODE_NOT_UNIQUE == postService.checkPostCodeUnique(post)) {
			ApiAssert.failure(ErrorCodeEnum.BAD_REQUEST.convert("新增岗位'" + post.getPostName() + "'失败，岗位编码已存在"));
		}
		post.setCreateBy(ShiroUtils.getLoginName());
		postService.insertPost(post);
	}

	/**
	 * 修改岗位
	 */
	@GetMapping("/edit/{postId}")
	public String edit(@PathVariable("postId") Long postId, ModelMap mmap) {
		mmap.put("post", postService.selectPostById(postId));
		return prefix + "/edit";
	}

	/**
	 * 修改保存岗位
	 */
	@ResponseBody
	@PostMapping("/edit")
	@RequiresPermissions("system:post:edit")
	@Log(title = "岗位管理", businessType = BusinessType.UPDATE)
	public void editSave(@Validated SysPost post) {
		if (UserConstants.POST_NAME_NOT_UNIQUE == postService.checkPostNameUnique(post)) {
			ApiAssert.failure(ErrorCodeEnum.BAD_REQUEST.convert("修改岗位'" + post.getPostName() + "'失败，岗位名称已存在"));
		} else if (UserConstants.POST_CODE_NOT_UNIQUE == postService.checkPostCodeUnique(post)) {
			ApiAssert.failure(ErrorCodeEnum.BAD_REQUEST.convert("修改岗位'" + post.getPostName() + "'失败，岗位编码已存在"));
		}
		post.setUpdateBy(ShiroUtils.getLoginName());
		postService.updatePost(post);
	}

	/**
	 * 校验岗位名称
	 */
	@PostMapping("/checkPostNameUnique")
	@ResponseBody
	public int checkPostNameUnique(SysPost post) {
		return postService.checkPostNameUnique(post);
	}

	/**
	 * 校验岗位编码
	 */
	@PostMapping("/checkPostCodeUnique")
	@ResponseBody
	public int checkPostCodeUnique(SysPost post) {
		return postService.checkPostCodeUnique(post);
	}
}
